<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'分销订单列表'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=trim($_RQ['ordersn']);
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('order',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['teamid']=$this->pdo_getcolumn('member',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']),'fromuser');
			$item['team']=cfc('core')->member_show($item['teamid']);
			$orderin=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ordersn'=>$item['ordersn']));
			$item['goodsid']=pdo_getcolumn('jy_weishop_order_goods',array('orderid'=>$orderin['id']),'goodsid');
			$goods=pdo_get('jy_weishop_goods',array('id'=>$item['goodsid']),array('image','title'));
			$item['goodstitle']=$goods['title'];
			$item['goodsimage']=$goods['image'];
			$item['detail']='/commission/list/ordersn='.$item['ordersn'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'list'=>array('data'=>'list','tip'=>'上级低于下级，不产生分红；','edit'=>false,'list'=>array(
			array('name'=>'member','title'=>'下单客户','type'=>'member'),
			array('name'=>'team','title'=>'直推股东','type'=>'member'),
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('type'=>'img_string','title'=>'商品名称_Goods title','img'=>'goodsimage',
			'data'=>array(
				array('title'=>'','name'=>'goodstitle'),
				//array('title'=>'价格：￥','name'=>'price')
				)
			),
			array('type'=>'img_string','title'=>'订单金额',
			'data'=>array(
				array('title'=>'订单金额：￥','name'=>'order_price'),
				array('title'=>'计算佣金的金额：￥','name'=>'commission_price')
				)
			),
			array('name'=>'commission_money','title'=>'产生分红','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','name'=>'detail','title'=>'佣金明细'),
		)),
		'search'=>array(
			array('name'=>'ordersn','width'=>10,'title'=>'订单号_Order ID','type'=>'string'),
			array('type'=>'choose','width'=>18,'action'=>'jy_merch/manage.get_member/','name'=>'mid','title'=>'选择会员'),
			array('name'=>'createtime','width'=>15,'title'=>'下单时间','type'=>'time_range'),
		),
	);
	$return['htmls']=$htmls;
	$return['levels']=$this->getall('level',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('wechat'=>'微信支付','credit'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	$order=$this->pdo_getall('order',$params,'','','id desc');
	foreach($order as &$v){
		$teamid=$this->pdo_getcolumn('commission',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'ordersn'=>$v['ordersn'],'type'=>0),'mid');
		$team=cfc('core')->member_show($teamid);
		$v['teamname']=$team['nickname'];
		$orderin=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ordersn'=>$v['ordersn']));
		$member=pdo_get('core_members',array('id'=>$v['mid']),array('nickname','mobile'));
		$v['nickname']=$member['nickname'];
		$v['usermobile']=$member['mobile'];	
		$address=pdo_get('core_address',array('id'=>$orderin['addressid'],'uniacid'=>$_SESSION['uniacid']));
		$v['address']=model('core/member')->get_address_str($orderin['addressid']);
		$v['mobile']=$address['mobile'];
		$v['realname']=$address['realname']?:$address['name'];
		$v['status']=$status[$orderin['status']];
		$v['paytype']=$paytype[$orderin['paytype']];
		$v['ispay']=$ispay[$orderin['ispay']];
		if(!$v['nickname']){
			$v['nickname']='匿名用户';
		}
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		$v['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$orderin['storeid']),'title');
		$v['storetitle']=$v['storetitle']?:'无';
		$goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$orderin['id']),array('goodsid','optionid'));
		foreach($goods as $k=>$gv){
			$goodstitle=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['goodsid']),'title');
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$selespename=implode("，",$specs);
			}else{
				$selespename='无规格';
			}
			$num=$k+1;
			$v['goodstitle'] .='【商品'.$num.'】['.$selespename.']'.$goodstitle;
		}
	}
	$header=array(
		'ordersn'=>'订单号',
		'storetitle'=>'所属门店',
		'goodstitle'=>'商品名称',
		'nickname'=>'买家昵称',
		'usermobile'=>'买家电话',
		'order_price'=>'金额',
		'status'=>'订单状态',
		'ispay'=>'支付状态',
		'paytype'=>'支付方式',
		'realname'=>'收货人姓名',
		'address'=>'收货地址',
		'mobile'=>'收货人电话',
		'teamname'=>'直推股东',
		'commission_money'=>'直推分红',
	);
	cfc('tools')->export2excel($header,$order,'股东订单');
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		$this->delete('order',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('order',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>